import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

// 路由配置
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    name: 'Home',
    component: () => import('@/views/Home/index.vue'),
    meta: {
      title: 'IKeySoft - 首页',
      description: '企业核心价值主张与关键业务指标展示'
    }
  },
  {
    path: '/solution',
    name: 'Solution',
    component: () => import('@/views/Solutions/index.vue'),
    meta: {
      title: 'IKeySoft - 解决方案',
      description: '详细呈现行业解决方案与技术架构'
    }
  },
  {
    path: '/product',
    name: 'Product',
    component: () => import('@/views/Product/index.vue'),
    meta: {
      title: 'IKeySoft - 产品中心',
      description: '系统化展示产品矩阵与功能特性'
    }
  },
  {
    path: '/legal',
    name: 'Legal',
    component: () => import('@/views/Legal/index.vue'),
    meta: {
      title: 'IKeySoft - 法律协议',
      description: '用户协议、隐私政策等法律文档'
    }
  },
  {
    path: '/service',
    name: 'Service',
    component: () => import('@/views/Service/index.vue'),
    meta: {
      title: 'IKeySoft - 客户服务',
      description: '专业的客户服务支持'
    }
  },
  {
    path: '/contact',
    name: 'Contact',
    component: () => import('@/views/Contact/index.vue'),
    meta: {
      title: 'IKeySoft - 联系我们',
      description: '联系方式和在线客服'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound/index.vue'),
    meta: {
      title: '页面未找到 - IKeySoft'
    }
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta?.title) {
    document.title = to.meta.title as string
  }
  
  // 设置页面描述
  if (to.meta?.description) {
    const metaDescription = document.querySelector('meta[name="description"]')
    if (metaDescription) {
      metaDescription.setAttribute('content', to.meta.description as string)
    }
  }
  
  next()
})

export default router